package com.jiuyou.storeController.technician;

import com.jiuyou.storeController.common.StoreBaseController;
import com.ruoyi.common.core.controller.HandleVideoSrc;
import com.ruoyi.common.core.domain.entity.StoreUserRole;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.technician.domain.StoreTechnician;
import com.ruoyi.technician.service.IStoreTechnicianService;
import com.ruoyi.technicianOpus.domain.StoreTechnicianOpus;
import com.ruoyi.technicianOpus.service.IStoreTechnicianOpusService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chengliang
 * @date 2021/12/10
 **/
@RestController
@RequestMapping("/store/technicianOpus")
@Api(tags = {"技师作品控制器"})
public class TechnicianOpusStoreController extends StoreBaseController {
    @Autowired
    private IStoreTechnicianOpusService opusService;
    @Autowired
    private IStoreTechnicianService technicianService;
    @Autowired
    private ISysConfigService configService;

    @GetMapping("/getList/{id}/{type}")
    public List<StoreTechnicianOpus> getList(@PathVariable Long id, @PathVariable String type) {
        StoreUserRole storeUserRole = getStoreUserRole();
//        StoreTechnician storeTechnician = technicianService.selectStoreTechnicianById(id);
//        if (null == storeTechnician) {
//            return null;
//        }
        StoreTechnicianOpus op = new StoreTechnicianOpus();
        op.setTechId(id);
        op.setStoreId(storeUserRole.getStoreId());
        if(!"all".equals(type)){
            op.setOpusType(type);
        }
        List<StoreTechnicianOpus> storeTechnicianOpuses = opusService.selectStoreTechnicianOpusList(op);
        return storeTechnicianOpuses;
    }
    @GetMapping("/getTechnicianOpusList/{type}")
    public List<StoreTechnicianOpus> getTechnicianOpusList( @PathVariable String type) {
        StoreUserRole storeUserRole = getStoreUserRole();
        StoreTechnician storeTechnician=getStoreTechnician();
        Long id=null;
        if(null!=storeTechnician){
            id=storeTechnician.getId();
        }
        StoreTechnicianOpus op = new StoreTechnicianOpus();
        op.setTechId(id);
        op.setStoreId(storeUserRole.getStoreId());
        if(!"all".equals(type)){
            op.setOpusType(type);
        }
        List<StoreTechnicianOpus> storeTechnicianOpuses = opusService.selectStoreTechnicianOpusList(op);
        return storeTechnicianOpuses;
    }
    @GetMapping("/getOne/{id}")
    public StoreTechnicianOpus getOne(@PathVariable Long id) {
//        StoreUserRole storeUserRole = getStoreUserRole();
        StoreTechnicianOpus storeTechnicianOpuses = opusService.selectStoreTechnicianOpusById(id);
        return storeTechnicianOpuses;
    }
    @GetMapping("/del/{id}")
    public Integer del(@PathVariable Long id) {
        StoreUserRole storeUserRole = getStoreUserRole();
//        StoreTechnician storeTechnician = technicianService.selectStoreTechnicianById(id);
//        if (null == storeTechnician) {
//            return null;
//        }
        int i = opusService.deleteStoreTechnicianOpusById(id);
        return i;
    }
    @PutMapping("/create")
    public StoreTechnicianOpus create(@RequestBody StoreTechnicianOpus opus) {
        StoreUserRole storeUserRole = getStoreUserRole();
        Long techId=opus.getTechId();
        if(null==techId){
            StoreTechnician storeTechnician=getStoreTechnician();
            if(null!=storeTechnician){
                techId=storeTechnician.getId();
            }
            if(null==techId){
                throw  new CustomException("无权操作");
            }
            opus.setTechId(techId);
        }
        StoreTechnician storeTechnician = technicianService.selectStoreTechnicianById(techId);
        opus.setTechImage(storeTechnician.getTechImage());
        opus.setTechName(storeTechnician.getTechName());
        opus.setStoreId(storeUserRole.getStoreId());
        if(StringUtils.isNotEmpty(opus.getOpusVideoUrl())){
            Map<String, String> val = configService.getVal();
            opus.setVideoCover(HandleVideoSrc.handle(val.get("key"),val.get("id"),val.get("region"),val.get("bn"),opus.getOpusVideoUrl()));
        }
        opusService.insertStoreTechnicianOpus(opus);
        return opus;
    }
    @PutMapping("/edit")
    public StoreTechnicianOpus edit(@RequestBody StoreTechnicianOpus opus) {
        StoreUserRole storeUserRole = getStoreUserRole();
        StoreTechnician storeTechnician = technicianService.selectStoreTechnicianById(opus.getTechId());
        opus.setTechImage(storeTechnician.getTechImage());
        opus.setTechName(storeTechnician.getTechName());
        opus.setStoreId(storeUserRole.getStoreId());
        if(StringUtils.isNotEmpty(opus.getOpusVideoUrl())){
            Map<String, String> val = configService.getVal();
            opus.setVideoCover(HandleVideoSrc.handle(val.get("key"),val.get("id"),val.get("region"),val.get("bn"),opus.getOpusVideoUrl()));
        }
        opusService.updateStoreTechnicianOpus(opus);
        return opus;
    }
}
